package com.netease.financial.common.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import java.io.File;

/**
 * @author yufeng.wu
 */
public class PackageUtils {

    // Install an apk
    public static void installApk(Context context, File apkFile) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    public static boolean hasNewVersionApp(Context context, int newVersionCode) {
        int oldVersionCode = PackageUtils.getCurrentApkVersionCode(context);
        return newVersionCode > oldVersionCode;
    }

    // Get the version of current installed apk
    public static String getCurrentApkVersionName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static int getCurrentApkVersionCode(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            return info.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }

    // Get the version of an downloaded apk
    public static String getLocalApkVersionName(Context context, String apkPath) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageArchiveInfo(apkPath, 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
//            File file = new File(apkPath);
//            if (file.exists()) {
//                file.delete();
//            }
            return "";
        }
    }

    public static int getLocalApkVersionCode(Context context, String apkPath) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(apkPath, 0);
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
//            File file = new File(apkPath);
//            if (file.exists()) {
//                file.delete();
//            }
            return 0;
        }
    }

    public static String getStringMetaData(Context context, String metaKey) {
        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo appInfo = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            return appInfo.metaData.getString(metaKey);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }
}
